package com.bkgy.controller.tcmPlSpeedRules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.tcmPlSpeedRules.entity.TcmPlSpeedRules;
import com.bkgy.controller.tcmPlSpeedRules.entity.pdo.TcmPlSpeedRulesQueryReq;
import com.bkgy.controller.tcmPlSpeedRules.mapper.ITcmPlSpeedRulesMapper;
import com.bkgy.controller.tcmPlSpeedRules.service.ITcmPlSpeedRulesService;
import com.bkgy.sys.common.entity.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TcmPlSpeedRulesServiceImpl extends ServiceImpl<ITcmPlSpeedRulesMapper,TcmPlSpeedRules> implements ITcmPlSpeedRulesService {

	@Override
	public ResultData<String> saveTcmPlSpeedRules(TcmPlSpeedRules obj) {
		boolean res = false;
		try {
			obj.setUpdateTime(new Date());
			res = save(obj);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return ResultData.ResultDataSuccess(res ? "SUCCESS" : "FAIL");
	}

	@Override
	public ResultData<String> updateTcmPlSpeedRules(TcmPlSpeedRules obj) {
		obj.setUpdateTime(new Date());
		boolean res = updateById(obj);
		return ResultData.ResultDataSuccess(res ? "SUCCESS" : "FAIL");
	}

	@Override
	public ResultData<String> deleteTcmPlSpeedRules(TcmPlSpeedRules obj) {
		boolean res = removeById(obj);
		return ResultData.ResultDataSuccess(res ? "SUCCESS" : "FAIL");
	}

	@Override
	public ResultData selectPage(TcmPlSpeedRulesQueryReq req) {
		Page<TcmPlSpeedRules> p = new Page<>(req.getPageIndex(), req.getPageSize());
		LambdaQueryWrapper<TcmPlSpeedRules> wrapper = new LambdaQueryWrapper<>();
		if(StringUtils.isNotEmpty(req.getGradeStr())){
			wrapper.like(TcmPlSpeedRules::getGradeStr, req.getGradeStr());
		}
		IPage<TcmPlSpeedRules> data = page(p, wrapper);
		return ResultData.ResultDataSuccess(data.getRecords(), (int) data.getTotal());
	}

}
